package wo.bsys.test;

import java.util.Arrays;
import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import wo.bsys.po.Menu;
import wo.bsys.po.Role;
import wo.bsys.po.User;
import wo.bsys.repository.DictionaryRepository;
import wo.bsys.repository.MenuRepository;
import wo.bsys.repository.RoleRepository;
import wo.bsys.repository.UserRepository;
import wo.common.util.WoUtil;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class BSysInitializer {
	private Logger LOG = LogManager.getLogger(BSysInitializer.class);

	@Resource
	private MenuRepository menuRepository;

	@Resource
	private DictionaryRepository dictionaryRepository;

	@Resource
	private RoleRepository roleRepository;

	@Resource
	private UserRepository userRepository;

	private final static String PWD = "123456";
	
	/**
	 * 初始化系统数据
	 */
	@Test
	public void initData() {
		// 系统管理
		// 父菜单
		Menu m = new Menu("esys", "系统管理", "00", "fa-bars", null, null);
		this.menuRepository.save(m);
		// 子菜单
		Menu c1 = new Menu("esys-menu", "菜单", "0010", "fa-gears", "menu/main.jsp", m);
		this.menuRepository.save(c1);
		Menu c2 = new Menu("esys-role", "角色", "0020", "fa-group", "role/main.jsp", m);
		this.menuRepository.save(c2);
		Menu c3 = new Menu("esys-user", "用户", "0030", "fa-user", "user/main.jsp", m);
		this.menuRepository.save(c3);
		Menu c4 = new Menu("esys-dictionary", "数据字典", "0040", "fa-book", "dictionary/main.jsp", m);
		this.menuRepository.save(c4);
		// 角色：系统管理员
		Role admin = new Role();
		admin.setId("admin");
		admin.setName("系统管理员");
		roleRepository.save(admin);
		// 管理员用户
		User userAdmin = new User();
		userAdmin.setId("admin");
		userAdmin.setLoginName("admin");
		userAdmin.setPassword(WoUtil.getMD5(userAdmin.getId(), PWD));
		userAdmin.setCreateTime(new Date());
		userAdmin.setPasswordTime(new Date());
		userAdmin.setRoles(Arrays.asList(admin));
		userRepository.save(userAdmin);
		#foreach ($moduleEntry in ${modules.entrySet()})
		#set($modIndex = ${velocityCount})
		// 父菜单：${moduleEntry.key}管理
		Menu ${moduleEntry.key} = new Menu("${moduleEntry.key}", "${moduleEntry.key}管理", "${modIndex}0", "fa-exclamation-circle", null, null);
		this.menuRepository.save(${moduleEntry.key});
		// ${moduleEntry.key}子菜单
		#foreach ($entity in ${moduleEntry.value})
		#if(!$entity.isWeak())
		Menu ${entity.module}${entity.entityName} = new Menu("${entity.module}-${entity.entityNameLowerFirstChar}", "${entity.entityCN}", "${modIndex}0${velocityCount}0", "fa-gears", "${entity.entityNameLowerFirstChar}/main.jsp", ${moduleEntry.key});
		this.menuRepository.save(${entity.module}${entity.entityName});
		#end
		#end
		// 角色：${moduleEntry.key}
		Role ${moduleEntry.key}Role = new Role();
		${moduleEntry.key}Role.setId("${moduleEntry.key}");
		${moduleEntry.key}Role.setName("${moduleEntry.key}");
		${moduleEntry.key}Role.setMenus(Arrays.asList(${moduleEntry.key} #foreach ($entity in ${moduleEntry.value})#if(!$entity.isWeak()) , ${entity.module}${entity.entityName} #end #end));
		roleRepository.save(${moduleEntry.key}Role);
		// 用户：${moduleEntry.key}
		User ${moduleEntry.key}User = new User();
		${moduleEntry.key}User.setId("${moduleEntry.key}");
		${moduleEntry.key}User.setLoginName("${moduleEntry.key}");
		${moduleEntry.key}User.setPassword(WoUtil.getMD5(${moduleEntry.key}User.getId(), PWD));
		${moduleEntry.key}User.setCreateTime(new Date());
		${moduleEntry.key}User.setPasswordTime(new Date());
		${moduleEntry.key}User.setRoles(Arrays.asList(${moduleEntry.key}Role));
		userRepository.save(${moduleEntry.key}User);
		#end
	}
}
